package com.hddf.project.action;

import java.util.List;

import com.hddf.project.base.BaseAction;
import com.hddf.project.pojos.Project;
import com.hddf.project.pojos.ProjectPlan;
import com.hddf.project.pojos.ProjectPlanItem;
import com.hddf.project.pojos.User;
import com.hddf.project.service.LogsService;
import com.hddf.project.service.PlanService;
import com.hddf.project.util.O;

public class PlanAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Integer planId;
	private String planName;

	private Integer planItemId;
	private Integer projectId;
	private Integer projectPlanId;
	private String projectName;

	private String projectIds;
	private String startTime;
	private String endTime;

	private String planExcelByPlanIdPath;

	/**
	 * 跳转到计划管理页面
	 * 
	 * @return
	 */
	public String jumpPlanManager() {
		setSuccessPath("/page/plan/plan_manager.html");
		return SUCCESS;
	}

	/**
	 * 跳转到计划详情页面
	 * 
	 * @return
	 */
	public String jumpPlanDetails() {
		setSuccessPath("/page/plan/plan_details.html");
		return SUCCESS;
	}

	/**
	 * 跳转到浏览计划页面
	 * 
	 * @return
	 */
	public String jumpBrowsePlanList() {
		setSuccessPath("/page/browse_project/plan_list.html");
		return SUCCESS;
	}

	/**
	 * 跳转到浏览计划配置页面
	 * 
	 * @return
	 */
	public String jumpBrowsePlanItemList() {
		setSuccessPath("/page/browse_project/plan_details.html");
		return SUCCESS;
	}

	/**
	 * 添加计划
	 */
	public void addPlan() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				if (O.ckSt(planName)) {
					PlanService ps = PlanService.getInService();
					ProjectPlan pp = ps.addPlan(planName);
					log(5, "add project_plan", pp.getId(), planName);

					ls = LogsService.getInService();
					ls.addLog(user, "添加计划，计划ID为：" + pp.getId() + "，计划名称为：" + planName, (short) 1);
				} else {
					state = "error";
					msg = "数据丢失，无效操作";
				}
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "添加失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 根据计划ID获取计划
	 */
	public void getPlanByPlanId() {
		try {
			if (O.ckSt(planId)) {
				PlanService ps = PlanService.getInService();
				ProjectPlan pp = ps.getPlanAndCountByPlanId(planId);
				msg = pp;
				log(5, "get project_plan by", planId);
			} else {
				state = "error";
				msg = "数据丢失，无效操作";
			}
		} catch (Exception e) {
			state = "error";
			msg = "添加失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 修改计划
	 */
	public void updatePlan() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				if (O.ckSt(planName) && 0 != planId) {
					PlanService ps = PlanService.getInService();
					ps.updatePlan(planId, planName);
					log(5, "update project_plan", planId, planName);
				} else {
					state = "error";
					msg = "数据丢失，无效操作";
				}

				ls = LogsService.getInService();
				ls.addLog(user, "修改计划，计划ID为：" + planId + "，计划名称为：" + planName, (short) 2);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "修改失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 删除计划
	 */
	public void deletePlan() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				if (0 != planId) {
					PlanService ps = PlanService.getInService();
					ps.deletePlan(planId);
					log(5, "delete project_plan", planId);

					ls = LogsService.getInService();
					ls.addLog(user, "删除计划，计划ID为：" + planId, (short) 3);
				} else {
					state = "error";
					msg = "数据丢失，无效操作";
				}
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "删除失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 获取全部计划
	 */
	public void getAllPlan() {
		try {
			PlanService ps = PlanService.getInService();
			List<ProjectPlan> ppList = ps.getAllPlan();
			msg = ppList;
			log(6, "get all plan");
		} catch (Exception e) {
			state = "error";
			msg = "操作失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 批量添加计划配置
	 */
	public void addBulkPlanItem() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				if (0 != planId && O.ckSt(projectIds)) {
					PlanService ps = PlanService.getInService();
					ps.addBulkPlanItem(planId, projectIds);
					planLog(planId, projectIds);
				} else {
					state = "error";
					msg = "数据丢失，无效操作";
				}

				ls = LogsService.getInService();
				ls.addLog(user, "批量加入计划，计划ID为：" + planId + "，项目ID为：" + projectIds, (short) 1);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "添加失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 添加计划配置
	 */
	public void addPlanItem() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				if (0 != planId && 0 != projectId) {
					PlanService ps = PlanService.getInService();
					ps.addPlanItem(planId, projectId);
					log(5, "add project_plan_item", planId, "项目ID为：" + projectId);
				} else {
					state = "error";
					msg = "数据丢失，无效操作";
				}

				ls = LogsService.getInService();
				ls.addLog(user, "加入计划，计划ID为：" + planId + "，项目ID为：" + projectId, (short) 1);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "添加失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 删除计划配置
	 */
	public void deleteProjectPlanItem() {
		try {
			User user = (User) getRequest().getAttribute("user");
			if (null != user) {
				if (0 != planItemId) {
					PlanService ps = PlanService.getInService();
					ps.deleteProjectPlanItem(planItemId);
					log(5, "delete project_plan_item by ProjectPlanItem id", planItemId);
				} else {
					state = "error";
					msg = "数据丢失，无效操作";
				}

				ls = LogsService.getInService();
				ls.addLog(user, "从计划中删除项目，项目ID为：" + projectId, (short) 3);
			} else {
				state = "error";
				msg = "请登录后在进行操作！";
			}
		} catch (Exception e) {
			state = "error";
			msg = "取消失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 根据计划ID获取计划配置中的项目
	 */
	public void getProjectListByPlanId() {
		try {
			PlanService ps = PlanService.getInService();
			if (0 != planId) {
				List<Project> pList = ps.getProjectListByPlanId(planId);
				msg = pList;
				log(6, "get project_plan_item by project_plan_id", planId);
			} else {
				state = "error";
				msg = "数据丢失，无效操作";
			}
			ps.getAllPlan();
			log(6, "get all plan");
		} catch (Exception e) {
			state = "error";
			msg = "删除失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 根据计划名称和分页查询计划
	 */
	public void getPlanListByNameAndPage() {
		try {
			PlanService ps = PlanService.getInService();
			page = (page - 1) * lengthData;
			if (O.ckSt(startTime)) {
				startTime = startTime + " 00:00:01";
			}
			if (O.ckSt(endTime)) {
				endTime = endTime + " 23:59:59";
			}
			List<ProjectPlan> ppList = ps.getPlanListDetails(planName, startTime, endTime, page, lengthData);
			msg = ppList;
			int result = ps.getPlansCountByNameAndPage(planName, startTime, endTime);
			map.put("pageCount", getPageCount(result));
			log(6, "get project_plan by project_plan_name or page");
		} catch (Exception e) {
			state = "error";
			msg = "操作失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 根据计划ID导出Excel
	 */
	public String planExcelByPlanId() {
		User user = (User) getRequest().getAttribute("user");
		if (null != user) {
			setPlanExcelByPlanIdPath("/page/plan/planExcelByPlanId.jsp?planId=" + planId);

			ls = LogsService.getInService();
			ls.addLog(user, "导出计划Excel，计划ID为：" + planId, (short) 4);

			return "planExcelByPlanId";
		} else {
			return "login";
		}
	}

	/**
	 * 获取项目可以列入的计划
	 */
	public void listPlan() {
		try {
			PlanService ps = PlanService.getInService();
			List<ProjectPlan> ppList = ps.getAllPlan();
			List<ProjectPlanItem> ppiList = ps.getPlanItemByProjectId(projectId);
			ppList = ps.listPlan(ppList, ppiList);
			msg = ppList;
			log(6, "listPlan : projectId is" + projectId);
		} catch (Exception e) {
			state = "error";
			msg = "操作失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 根据项目ID查看对应的计划
	 */
	public void listProjectPlanItem() {
		try {
			PlanService ps = PlanService.getInService();
			List<ProjectPlanItem> ppiList = ps.getPlanItemByProjectId(projectId);
			ppiList = ps.infoProjectPlanItem(ppiList);
			msg = ppiList;
			log(6, "listPlan : projectId is" + projectId);
		} catch (Exception e) {
			state = "error";
			msg = "操作失败";
			e.printStackTrace();
		} finally {
			map.put("state", state);
			map.put("msg", msg);
			printHttpServletResponse(getJson(map));
		}
	}

	/**
	 * 跳转到计划详情页面
	 * 
	 * @return
	 */
	public String jumpPlanItem() {
		setSuccessPath("/page/project/plan_item.jsp");
		return SUCCESS;
	}

	public Integer getPlanId() {
		return planId;
	}

	public void setPlanId(Integer planId) {
		this.planId = planId;
	}

	public String getPlanName() {
		return planName;
	}

	public void setPlanName(String planName) {
		this.planName = planName;
	}

	public Integer getPlanItemId() {
		return planItemId;
	}

	public void setPlanItemId(Integer planItemId) {
		this.planItemId = planItemId;
	}

	public Integer getProjectId() {
		return projectId;
	}

	public void setProjectId(Integer projectId) {
		this.projectId = projectId;
	}

	public Integer getProjectPlanId() {
		return projectPlanId;
	}

	public void setProjectPlanId(Integer projectPlanId) {
		this.projectPlanId = projectPlanId;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getProjectIds() {
		return projectIds;
	}

	public void setProjectIds(String projectIds) {
		this.projectIds = projectIds;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getPlanExcelByPlanIdPath() {
		return planExcelByPlanIdPath;
	}

	public void setPlanExcelByPlanIdPath(String planExcelByPlanIdPath) {
		this.planExcelByPlanIdPath = planExcelByPlanIdPath;
	}

}